<template>
  <div class="add">
    <el-dialog :visible="isShow" fullscreen @close="closeEvent">
      <template #title>
        <div class="blue">
          <span>{{ { add: '新增', edit: '编辑' }[mode] }}题库测试</span>
        </div>
      </template>
      <el-form
        label-width="80px"
        class="form"
        ref="form"
        :rules="rules"
        :model="form"
      >
        <el-form-item label="学科" prop="subject">
          <el-select v-model="form.subject" placeholder="请选择">
            <el-option
              v-for="item in subList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阶段" prop="step">
          <el-select v-model="form.step" placeholder="请选择">
            <el-option
              v-for="(item, key, index) in initData.stepList"
              :key="index"
              :label="item"
              :value="+key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业" prop="enterprise">
          <el-select v-model="form.enterprise" placeholder="请选择">
            <el-option
              v-for="item in entList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-cascader
            size="large"
            :options="options"
            v-model="form.city"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="题型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="(item, key, index) in initData.typeList"
              :key="index"
              :label="+key"
              >{{ item }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="难度" prop="difficulty">
          <el-radio-group v-model="form.difficulty">
            <el-radio
              v-for="(item, key, index) in initData.diffList"
              :key="index"
              :label="+key"
              >{{ item }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <hr />
        <el-form-item label="试题标题" class="button" prop="title">
          <quillEditor v-model="form.title" class="fwb"></quillEditor>
        </el-form-item>
        <el-form-item
          :label="initData.typeList[form.type]"
          :prop="radioObj[form.type]"
        >
          <radio :form="form" @clearForm="clearForm"></radio>
        </el-form-item>
        <el-form-item label="解析视频" prop="video">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl + '/question/upload'"
            :show-file-list="false"
            :on-success="handleVideoSuccess"
            :before-upload="beforeUploadVideo"
          >
            <video
              v-if="form.video"
              :src="uploadUrl + '/' + form.video"
              class="avatar"
              controls
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <div>只能上传视频格式文件</div>
        </el-form-item>
        <el-form-item label="答案解析" prop="answer_analyze">
          <quillEditor v-model="form.answer_analyze"></quillEditor>
        </el-form-item>
        <hr />
        <el-form-item label="试题备注" class="button" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import Radio from './radio.vue'
/* provinceAndCityData是省市二级联动数据（不带“全部”选项）
regionData是省市区三级联动数据（不带“全部”选项）
provinceAndCityDataPlus是省市区二级联动数据（带“全部”选项）
regionDataPlus是省市区三级联动数据（带“全部”选项） */
import { regionData } from 'element-china-area-data'
import { questionAdd, questionEdit } from '@/api/question.js'
export default {
  components: { quillEditor, Radio },
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    subList: {
      type: Array,
      default: () => []
    },
    entList: {
      type: Array,
      default: () => []
    },
    initData: {
      type: Object,
      default: () => {}
    },
    mode: {
      type: String,
      default: 'add'
    }
  },
  model: {
    prop: 'isShow'
  },
  created () {
    console.log('cre', this.form)
  },
  data () {
    return {
      options: regionData,
      form: {
        title: '', // 标题
        subject: '', // 学科id标识
        step: '', // 阶段1、初级 2、中级 3、高级
        enterprise: '', // 是 企业id标识
        city: [], // 是 array[省、市、区县]
        type: 1, // 是 string题型 1单选 、2多选 、3简答
        difficulty: '', // 是 题目难度 1简单 、2一般 、3困难
        single_select_answer: '', // 是 单选题答案
        multiple_select_answer: [], // 是 array多选题答案
        short_answer: '', // 是 简答题答案
        video: '', // 否 解析视频地址
        answer_analyze: '', // 是 答案解析
        remark: '', // 是 答案备注
        select_options: [
          {
            label: 'A',
            text: '',
            image: ''
          },
          {
            label: 'B',
            text: '',
            image: ''
          },
          {
            label: 'C',
            text: '',
            image: ''
          },
          {
            label: 'D',
            text: '',
            image: ''
          }
        ] // 是 array 选项，介绍，图片介绍
      },
      radio: 3,
      radio2: 6,
      uploadUrl: process.env.VUE_APP_URL,
      rules: {
        subject: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        step: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        enterprise: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        city: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        type: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        difficulty: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        title: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        answer_analyze: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        remark: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        single_select_answer: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        multiple_select_answer: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        short_answer: [{ required: true, message: '必填', trigger: 'change' }]
      },
      radioObj: {
        1: 'single_select_answer',
        2: 'multiple_select_answer',
        3: 'short_answer'
      }
    }
  },
  methods: {
    closeEvent () {
      this.$emit('input', false)
      this.form = {
        title: '', // 标题
        subject: '', // 学科id标识
        step: '', // 阶段1、初级 2、中级 3、高级
        enterprise: '', // 是 企业id标识
        city: '', // 是 array[省、市、区县]
        type: 1, // 是 string题型 1单选 、2多选 、3简答
        difficulty: 1, // 是 题目难度 1简单 、2一般 、3困难
        single_select_answer: '', // 是 单选题答案
        multiple_select_answer: [], // 是 array多选题答案
        short_answer: '', // 是 简答题答案
        video: '', // 否 解析视频地址
        answer_analyze: '', // 是 答案解析
        remark: '', // 是 答案备注
        select_options: [
          {
            label: 'A',
            text: '',
            image: ''
          },
          {
            label: 'B',
            text: '',
            image: ''
          },
          {
            label: 'C',
            text: '',
            image: ''
          },
          {
            label: 'D',
            text: '',
            image: ''
          }
        ] // 是 array 选项，介绍，图片介绍
      }
      // 清除验证
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    // 验证视频格式和视频大小
    beforeUploadVideo (file) {
      const isLt8M = file.size / 1024 / 1024 < 8
      const arr = [
        'video/mp4',
        'video/ogg',
        'video/flv',
        'video/avi',
        'video/wmv',
        'video/rmvb'
      ]
      if (arr.indexOf(file.type) === -1) {
        this.$message.error('请上传正确的视频格式')
        return false
      }
      if (!isLt8M) {
        this.$message.error('上传视频大小不能超过10MB哦!')
        return false
      }
    },
    // 上传成功
    handleVideoSuccess (res, file) {
      // 获取上传地址
      this.form.video = res.data.url
    },
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            await questionAdd(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            console.log('修改', this.form)
            await questionEdit(this.form)
            this.$message.success('修改成功')
          }
          this.closeEvent()
          this.$emit('getData')
        }
      })
    },
    handleChange (val) {
      this.form.city = val
    },
    clearForm (val) {
      this.$refs.form.validateField([val])
    }
  }
}
</script>

<style scoped lang="scss">
.add {
  padding: 0 !important;
  ::v-deep .el-dialog__header {
    padding: 0;
    i.el-dialog__close.el-icon.el-icon-close {
      color: #fff;
    }
  }
  .blue {
    width: 100%;
    height: 54px;
    line-height: 54px;
    color: #fff;
    padding-left: 18px;
    background: -webkit-linear-gradient(left, #03bffa, #119afa);
  }
  .form {
    width: 835px;
    margin: 0 auto;
    ::v-deep .el-upload.el-upload--text {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
    }
    .avatar-uploader .el-upload {
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .button {
      margin-top: 20px;
    }
    .fwb {
      width: 100%;
    }
  }
}
</style>
